/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.event;

import com.common.json.EventLibraryJson;
import com.entities.EventInfo;
import com.entities.EventLibrary;
import com.model.EventLibraryModel;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.json.simple.JSONObject;

/**
 *
 * @author Tuan Long
 */
@Named(value = "updateEvent")
@RequestScoped
public class updateEvent {

    /**
     * Creates a new instance of updateEvent
     */
    public updateEvent() {
    }

    private static int eventID;
    private EventLibrary eventLibrary;

    public EventLibrary getEventLibrary() {
        return eventLibrary;
    }

    private int id;

    public int getId() {
        fillEventInfomation();
        return id;
    }

    private void fillEventInfomation() {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String eventId = req.getParameter("eventId");
        try {
            id = Integer.parseInt(eventId);
            if (id > 0) {
                EventLibraryModel model = new EventLibraryModel();
                eventLibrary = model.getEventUpdateContentById(id);
                if (eventLibrary != null) {
                    eventID = id;
                    name = eventLibrary.getEventName();
                    description = eventLibrary.getDescription();
                    type = eventLibrary.getEventType();
                    timeS = eventLibrary.getTimeStart();
                    timeE = eventLibrary.getTimeEnd();
                    avatar = eventLibrary.getAvatar();
                    shortdes = eventLibrary.getShortdes();
                    created = eventLibrary.getCreated();
                    address = eventLibrary.getAddress();
                }
            }
        } catch (Exception e) {
        }
    }

    public void setId(int id) {
        this.id = id;
    }
    private String name;
    private String description;
    private String type;
    private String timeS;
    private String timeE;
    private String avatar;
    private String shortdes;
    private String created;
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getShortdes() {
        return shortdes;
    }

    public void setShortdes(String shortdes) {
        this.shortdes = shortdes;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTimeS() {
        return timeS;
    }

    public void setTimeS(String timeS) {
        this.timeS = timeS;
    }

    public String getTimeE() {
        return timeE;
    }

    public void setTimeE(String timeE) {
        this.timeE = timeE;
    }

    private JSONObject getJsonEventMessage() {
        if (eventID > 0) {
            HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            name = req.getParameter("eventName");
            description = req.getParameter("description");
            type = req.getParameter("eventType");
            timeS = req.getParameter("timeS");
            timeE = req.getParameter("timeE");
            avatar = req.getParameter("avatar");
            shortdes = req.getParameter("shortdes");
            //address = req.getParameter("address");
            address = "fix data";
            EventInfo ei = new EventInfo();
            ei.setAvatar(avatar);
            ei.setShortdes(shortdes);
            ei.setAddress(address);
            EventLibrary event = new EventLibrary(eventID, name, description, type, timeS, timeE);
            JSONObject json = EventLibraryJson.convertEventLibraryAndInfoDataToJsonObject(event, ei);
            return json;
        }
        return null;
    }

    /**
     * @createEvent.createNewEvent: use EJB Remote insert new record
     * EventLibrary into DB
     * @return String
     */
    public String updateEvent() {
        JSONObject json = getJsonEventMessage();
        if (json != null) {
            String jsonstr = json.toString();
            EventLibraryModel model = new EventLibraryModel();
            boolean b = model.updateEvent(jsonstr);
            if (b) {
                return "events?faces-redirect=true";
            }
        }
        return "error?faces-redirect=true";
    }

}
